package org.matchit.converter;

import java.util.HashMap;
import java.util.Map;

import org.springframework.core.convert.converter.Converter;

public class CompositeMapConverter<K, S, T> implements Converter<S, Map<K, T>> {
    private final Map<K, Converter<S, T>> converters = new HashMap<K, Converter<S, T>>();

    public CompositeMapConverter<K, S, T> entry(K key, Converter<S, T> converter) {
        converters.put(key, converter);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<K, T> convert(S source) {
        Map<K, T> map = new HashMap<K, T>();
        for (Map.Entry<K, Converter<S, T>> converterEntry : converters.entrySet()) {
            map.put(converterEntry.getKey(), converterEntry.getValue().convert(source));
        }
        return map;
    }

}
